package com.zyh.system.message.service.impl;

import com.zyh.system.message.dao.impl.MessageDaoImpl;
import com.zyh.system.message.domain.Message;
import com.zyh.system.message.service.MessageService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Queue;
import java.util.Scanner;

/**
 * @Author zyh
 * @Date 2024/7/31 18:28
 * @Description MessageServiceImpl实现类
 */

public class MessageServiceImpl implements MessageService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final MessageDaoImpl daoImpl = new MessageDaoImpl();

    /**
     * 展示所有信息
     *
     * @param queue
     */
    @Override
    public void showMessage(Queue<Message> queue) {
        if (queue.isEmpty()) {
            System.out.println("请先添加留言");
        } else {
            daoImpl.showMessage(queue);
        }
    }

    /**
     * 添加留言
     *
     * @param queue
     * @return
     */
    @Override
    public void addMessage(Queue<Message> queue) throws ParseException {
        System.out.println("请输入留言人姓名：");
        String uName = INPUT.next();
        System.out.println("请输入留言日期：(格式：yyyy-MM-dd)");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = INPUT.next();
        Date parse = sdf.parse(date);
        String format = sdf.format(parse);
        System.out.println("请输入留言标题：");
        String title = INPUT.next();
        System.out.println("请输入留言内容：");
        String content = INPUT.next();
        Message message = new Message(uName, format, title, content);
        boolean is_successful = daoImpl.addMessage(queue, message);
        if (is_successful) {
            System.out.println("留言成功！");
        } else {
            System.out.println("留言失败！");
        }
    }
}
